﻿using System.Collections.Generic;
using System.Linq;
using Amazon.SimpleNotificationService;
using Amazon.SimpleNotificationService.Model;
using NUnit.Framework;
using Rhino.Mocks;
using ShuttleBus.Transports.AmazonSqs.Clients;


namespace Tests.Transports.AmazonSqs.SnsWrapperTests
{
    public partial class SnsWrapperTests
    {
        [TestFixture]
        public class WhenQueryingTopics
        {
            private AmazonSimpleNotificationService _sns;
            private SnsWrapper _wrapper;

            [SetUp]
            public void SetUp()
            {
                _sns = MockRepository.GenerateMock<AmazonSimpleNotificationService>();
                _wrapper = new SnsWrapper(_sns);
            }

            [TearDown]
            public void TearDown()
            {
                _sns.VerifyAllExpectations();
            }

            [Test]
            public void No_Topics_Exist()
            {
                var response = new ListTopicsResponse
                {
                    ListTopicsResult = new ListTopicsResult
                    {
                        Topics = new List<Topic>()
                    }
                };

                _sns.Expect(s => s.ListTopics(Arg<ListTopicsRequest>.Is.NotNull)).Return(response);

                var results = _wrapper.QueryTopics("mymessage");

                Assert.AreEqual(0, results.Count());
            }

            [Test]
            public void No_Matching_Topics_Exist()
            {
                var topics = new List<Topic>
                                 {
                                     new Topic {TopicArn = "arn:aws:sns:us-east-1:123456789012:not-my-message"},
                                 };

                var response = new ListTopicsResponse
                                   {
                                       ListTopicsResult = new ListTopicsResult
                                                              {
                                                                  Topics = topics
                                                              }
                                   };

                _sns.Expect(s => s.ListTopics(Arg<ListTopicsRequest>.Is.NotNull)).Return(response);

                var results = _wrapper.QueryTopics("mymessage");

                Assert.AreEqual(0, results.Count());
            }

            [Test]
            public void Single_Topic_Exists_Matching_This_Name()
            {
                var topics = new List<Topic>
                                 {
                                     new Topic {TopicArn = "arn:aws:sns:us-east-1:123456789012:mymessage"},
                                 };

                var topicsResponse = new ListTopicsResponse
                {
                    ListTopicsResult = new ListTopicsResult
                    {
                        Topics = topics
                    }
                };
                _sns.Expect(s => s.ListTopics(Arg<ListTopicsRequest>.Is.Anything)).Return(topicsResponse);

                var results = _wrapper.QueryTopics("mymessage").ToArray();

                Assert.AreEqual(1, results.Count());
                Assert.AreEqual("arn:aws:sns:us-east-1:123456789012:mymessage", results[0].TopicArn);
            }

            [Test]
            public void Multiple_Topics_Exist_Matching_This_Name()
            {
                var topics = new List<Topic>
                                 {
                                     new Topic {TopicArn = "arn:aws:sns:us-east-1:123456789012:mymessage"},
                                     new Topic {TopicArn = "arn:aws:sns:us-east-1:987654321098:mymessage"},
                                 };

                var topicsResponse = new ListTopicsResponse
                {
                    ListTopicsResult = new ListTopicsResult
                    {
                        Topics = topics
                    }
                };
                _sns.Expect(s => s.ListTopics(Arg<ListTopicsRequest>.Is.Anything)).Return(topicsResponse);

                var results = _wrapper.QueryTopics("mymessage").ToArray();

                Assert.AreEqual(2, results.Count());
                Assert.AreEqual("arn:aws:sns:us-east-1:123456789012:mymessage", results[0].TopicArn);
                Assert.AreEqual("arn:aws:sns:us-east-1:987654321098:mymessage", results[1].TopicArn);
            }
        }
    }
}